package cn.javacart.jfinal.php.render;

import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.develnext.jphp.zend.ext.ZendExtension;

import php.runtime.env.CompileScope;

/**
 * PhpEnv工厂
 * @author farmer
 *
 */
public class PhpEnvFactory extends BasePooledObjectFactory<PhpEnvironment>{

	private CompileScope scope = new CompileScope();
	
	private AtomicInteger envId = new AtomicInteger();
	
	private String basePath = null;
	
	public PhpEnvFactory(boolean devMode, String basePath) {
		scope.setDebugMode(devMode);
		scope.registerExtension(new ZendExtension());
		this.basePath = basePath;
	}
	
	@Override
	public PhpEnvironment create() throws Exception {
		return new PhpEnvironment(envId.getAndIncrement(), scope,basePath);
	}

	@Override
	public PooledObject<PhpEnvironment> wrap(PhpEnvironment arg0) {
		return new DefaultPooledObject<PhpEnvironment>(arg0);
	}
	
}
